package com.sunchangming.rpcgen;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Random;

import junit.framework.Assert;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.sunchangming.test.ObjType;
import com.sunchangming.test.enumTest;
import com.sunchangming.test.typevars;
import com.sunchangming.test.typevars3;

public class MarshalTest {
	private static Random rand;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		rand = new Random();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	private static final String characters = "abcdefghijklmnopqrstuvwxzy0123456789;";

	private static String getRandString() {
		final int length = 64;
		char[] text = new char[length];
		for (int i = 0; i < length; i++) {
			text[i] = characters.charAt(rand.nextInt(characters.length()));
		}
		return new String(text);

	}

	@Test
	public void testenum() throws Exception {
		enumTest v=new enumTest();
		enumTest v2=new enumTest();
		v.setT1(20);
		v.setT2(ObjType.npc);
		
		byte[] ret;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		v.marshal(dos);
		dos.flush();
		baos.flush();
		ret = baos.toByteArray();
		baos.close();
		dos.close();

		ByteArrayInputStream bais = new ByteArrayInputStream(ret);
		DataInputStream dis = new DataInputStream(bais);

		v2.unmarshal(dis);
		bais.close();
		dis.close();
		
		Assert.assertEquals(v, v2);
	}
	@Test
	public void test1() throws Exception {
		typevars v = new typevars();
		typevars v2 = new typevars();
		v.setT1(true);
		v.setT2((byte) rand.nextInt(Byte.MAX_VALUE + 1));
		v.setT3(getRandString());
		v.setT4(rand.nextDouble());
		v.setT5(rand.nextFloat());
		v.setT6(rand.nextInt());
		v.setT8((short) rand.nextInt(1 << 16));
		v.setT7(rand.nextLong());
		v.setT9(false);

		byte[] ret;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		v.marshal(dos);
		dos.flush();
		baos.flush();
		ret = baos.toByteArray();
		baos.close();
		dos.close();

		ByteArrayInputStream bais = new ByteArrayInputStream(ret);
		DataInputStream dis = new DataInputStream(bais);

		v2.unmarshal(dis);
		bais.close();
		dis.close();

		assertTrue(v.getT1() == v2.getT1());
		assertEquals(v.getT2(), v2.getT2());
		assertEquals(v.getT3(), v2.getT3());
		assertEquals(v.getT4(), v2.getT4(), Double.MIN_NORMAL);
		assertEquals(v.getT5(), v2.getT5(), Float.MIN_NORMAL);
		assertEquals(v.getT6(), v2.getT6());
		assertEquals(v.getT7(), v2.getT7());
		assertEquals(v.getT8(), v2.getT8());
		assertEquals(v.getT9(), v2.getT9());

		assertEquals(v, v2);
		assertEquals(v.hashCode(), v2.hashCode());

	}
	
	@Test
	public void test4() throws Exception {
		typevars3 v = new typevars3();
		typevars3 v2 = new typevars3();
		v.setT1(true);
		v.setT2((byte) rand.nextInt(Byte.MAX_VALUE + 1));
		v.setT3(getRandString());
		v.setT4(rand.nextDouble());
		v.setT5(rand.nextFloat());
		v.setT6(rand.nextInt());
		v.setT8((short) rand.nextInt(1 << 16));
		v.setT7(rand.nextLong());
		v.setT9(false);
		v.setT10(ObjType.npc);
		v.setT88(rand.nextInt());

		byte[] ret;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		v.marshal(dos);
		dos.flush();
		baos.flush();
		ret = baos.toByteArray();
		baos.close();
		dos.close();

		ByteArrayInputStream bais = new ByteArrayInputStream(ret);
		DataInputStream dis = new DataInputStream(bais);

		v2.unmarshal(dis);
		bais.close();
		dis.close();

		assertTrue(v.getT1() == v2.getT1());
		assertEquals(v.getT2(), v2.getT2());
		assertEquals(v.getT3(), v2.getT3());
		assertEquals(v.getT4(), v2.getT4(), Double.MIN_NORMAL);
		assertEquals(v.getT5(), v2.getT5(), Float.MIN_NORMAL);
		assertEquals(v.getT6(), v2.getT6());
		assertEquals(v.getT7(), v2.getT7());
		assertEquals(v.getT8(), v2.getT8());
		assertEquals(v.getT9(), v2.getT9());
		assertEquals(v.getT88(), v2.getT88());
		
		assertEquals(v, v2);
		assertEquals(v.hashCode(), v2.hashCode());

	}

}
